/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doanlythuyet_javaoutlook.MyUserControl;

import BUS.MyMailBUS;
import doanlythuyet_javaoutlook.DoAnLyThuyet_JavaOutLookApp;
import doanlythuyet_javaoutlook.DoAnLyThuyet_JavaOutLookView;
import doanlythuyet_javaoutlook.EventListener_ClickChuotVaoCayDuyetFile;
import doanlythuyet_javaoutlook.MyUserControl.JDialog_NhapTenThuMuc;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import doanlythuyet_javaoutlook.MyUserControl.JTreeFromXMLFile;
import java.io.File;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class PopupMenu_MouseListerner_MyCayHienThiThuMuc implements MouseListener{
    private String m_HanhViDuocChon;
    private String sdataPath;
    //public String m_ThuMucDangChon;
    public PopupMenu_MouseListerner_MyCayHienThiThuMuc(String hanhVi){
        m_HanhViDuocChon = hanhVi;
        listenerList =
            new javax.swing.event.EventListenerList();
    }
    public void mouseClicked(MouseEvent e) {
       //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");

            //JOptionPane.showMessageDialog(null, "Hanh vi duoc chon: " + m_HanhViDuocChon + "xu ly code tai dong file popupmenu_mycay....java");
            String strPath = ((JPopupMenu) ((JMenuItem)e.getSource()).getParent()).getToolTipText();

            //String dataPath = DoAnLyThuyet_JavaOutLookApp.getCurdir() + "\\src\\Database\\";
           // dataPath = this.getClass().getResource("/Database/").getPath().replace("%20", " ").substring(6);
            //strPath = ;

            String fileName = "XML_ThuMucNguoiDung.xml";
            String strFileName = new File(".").getAbsolutePath() + "\\Database\\" + fileName;
            //JOptionPane.showMessageDialog(null, strFileName);
            JTreeFromXMLFile tree = new JTreeFromXMLFile(strFileName, false);
            //Xử Lý Thêm Thư Mục
            if (m_HanhViDuocChon.equals("Thêm mới")){
                try {

                    JDialog_NhapTenThuMuc dialog = new JDialog_NhapTenThuMuc(null, true);
                    dialog.setVisible(true);
                    String tenThuMuc = dialog.getTenThuMuc();
                    dialog.dispose();
                    if (tenThuMuc.equals(""))
                        return;
                    
                    tree.ThemThuMucMoiVaoXML(strPath, dialog.getTenThuMuc(), strFileName);
                    
                } catch (ParserConfigurationException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SAXException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TransformerConfigurationException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TransformerException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            

            else if (m_HanhViDuocChon.equals("Xóa")){
                try {
                    tree.XoaThuMucTrongXML(strPath, strFileName);
                } catch (ParserConfigurationException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SAXException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TransformerConfigurationException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TransformerException ex) {
                    Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            else if (m_HanhViDuocChon.equals("Đổi Tên")){

            try {
                JDialog_NhapTenThuMuc dialog = new JDialog_NhapTenThuMuc(null, true);
                dialog.setVisible(true);
                String tenThuMuc = dialog.getTenThuMuc();
                dialog.dispose();
                if (tenThuMuc.equals(""))
                    return;
                
                tree.DoiTenThuMucTrongXML(strPath, dialog.getTenThuMuc(), strFileName);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerException ex) {
                Logger.getLogger(PopupMenu_MouseListerner_MyCayHienThiThuMuc.class.getName()).log(Level.SEVERE, null, ex);
            }

            }
            //vẽ lại cây
            try {
            tree.TaoCay();
            DoAnLyThuyet_JavaOutLookView.getJTree_ThuMucTrenMay().getJTreeThuMucNguoiDung().setModel(tree.getJTreeThuMucNguoiDung().getModel());
        } catch (SAXException ex) {
            Logger.getLogger(PopupMenu_MouseListerner_MyBangDanhSachFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PopupMenu_MouseListerner_MyBangDanhSachFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(PopupMenu_MouseListerner_MyBangDanhSachFile.class.getName()).log(Level.SEVERE, null, ex);
        }

       //     ThemThuMucMoiVaoXML(strPath,"New folder");
    }


    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
    //<editor-fold defaultstate="collapsed" desc="Dung de phat sinh su kien">
    //Các hàm sau phục vụ cho việc gởi sự kiện click chuột vào bảng ra ngoài (tham khảo từ nhiều nguồn trên mạng)
    //http://www.exampledepot.com/egs/java.util/CustEvent.html
    // Tạo một listener list
    protected javax.swing.event.EventListenerList listenerList =
            new javax.swing.event.EventListenerList();

    /**
     * Phát sinh sử kiện click chuột vào tree
     * @param evt tham số cho sự kiện click chuột vào tree (ở đây là tên của file đang được chọn)
     */
    // This private class is used to fire MyEvents
    void initEvent_ClickChuotVaoCayDuyetFile(String evt) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == EventListener_ClickChuotVaoCayDuyetFile.class) {
                ((EventListener_ClickChuotVaoCayDuyetFile) listeners[i + 1]).Event_ClickChuotVaoCayDuyetFile_Occurred(evt);
            }
        }
    }

    /**
     * Đăng ký sự kiện cho classes
     * @param listener  Sự kiện cần đăng ký
     */
    public void addEventListener_ClickChuotVaoCayDuyetFile(EventListener_ClickChuotVaoCayDuyetFile listener) {
        listenerList.add(EventListener_ClickChuotVaoCayDuyetFile.class, listener);
    }

    /**
     * Gỡ bỏ sự kiện khỏi classes
     * @param listener  Sự kiện cần gỡ bỏ
     */
    public void delEventListener_ClickChuotVaoCayDuyetFile(EventListener_ClickChuotVaoCayDuyetFile listener) {
        listenerList.remove(EventListener_ClickChuotVaoCayDuyetFile.class, listener);
    }


    //</editor-fold>

}